﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Model;
using ExtAspNet;

namespace WebUI.LabManage
{
    public partial class ConsumableEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                switchPanel();
                bindData();
            }
            bn_close.OnClientClick = ExtAspNet.ActiveWindow.GetConfirmHideReference();
            btn_viewClose.OnClientClick = ExtAspNet.ActiveWindow.GetConfirmHideReference();
            bn_close2.OnClientClick = ExtAspNet.ActiveWindow.GetConfirmHideReference();
        }

        private void bindData()
        {
            string aciton = Request.QueryString["action"];
            if (aciton == "new")
            {
                dl_add_location.DataSource = BLL.Parameter.GetParamList("9");
                dl_add_location.DataTextField = "value";
                dl_add_location.DataValueField = "id";
                dl_add_location.DataBind();
            }
            if (aciton == "edit")
            {
                int id = int.Parse(Request.QueryString["id"]);
                Model.Consumable m = ConsumableManage.SelectOneByID(id);
                tb_edit_name.Text = m.name;
                tb_edit_remarks.Text = m.remarks;
                hf_edit_normal.Text = m.normal.ToString();
                hf_edit_bad.Text = m.bad.ToString();
                hf_edit_stock.Text = m.stock.ToString();
                dl_edit_location.DataSource = BLL.Parameter.GetParamList("9");
                dl_edit_location.DataTextField = "value";
                dl_edit_location.DataValueField = "id";
                dl_edit_location.DataBind();
                dl_edit_location.SelectedValue = m.location.ToString();
            }
            if (aciton == "view")
            {
                int id = int.Parse(Request.QueryString["id"]);
                Model.Consumable m = ConsumableManage.SelectOneByID(id);
                lb_view_name.Text = m.name;
                hf_view_location.Text = m.location.ToString();
                lb_view_location.Text = m.locationValue.value;
                lb_view_remarks.Text = m.remarks;
                lb_view_normal.Text = m.normal.ToString();
                lb_view_bad.Text = m.bad.ToString();
                lb_view_stock.Text = m.stock.ToString();
                dp_update_date.SelectedDate = DateTime.Now;
                string strWhere = string.Format(" cid = {0}",id);
                GridRecord.DataSource = ConsumableRecordManage.GetList(strWhere).Tables[0];
                GridRecord.DataBind();
            }
        }

        private void switchPanel()
        {
            string aciton = Request.QueryString["action"];
            switch (aciton)
            {
                case "new":
                    PageManager1.AutoSizePanelID = "addPanel";
                    addPanel.Hidden = false;
                    break;
                case "view":
                    PageManager1.AutoSizePanelID = "viewPanel";
                    viewPanel.Hidden = false;
                    break;
                case "edit":
                    PageManager1.AutoSizePanelID = "editPanel";
                    editPanel.Hidden = false;
                    break;
            }
        }

        protected void bnEdit_OnClick(object sender, EventArgs e)
        {
            int id = int.Parse(Request.QueryString["id"]);
            Model.Consumable m = new Model.Consumable();
            m.id = id;
            m.name = tb_edit_name.Text;
            m.normal = int.Parse(hf_edit_normal.Text);
            m.bad = int.Parse(hf_edit_bad.Text);
            m.stock = int.Parse(hf_edit_stock.Text);
            m.location = int.Parse(dl_edit_location.SelectedValue);
            m.remarks = tb_edit_remarks.Text;
            if (ConsumableManage.Edit(m))
            {
                PageContext.RegisterStartupScript(Alert.GetShowInTopReference("编辑成功！", "操作提示！", MessageBoxIcon.Information) + ActiveWindow.GetHidePostBackReference(Common.LinkService.RefreshString));
            }
            else
            {
                Alert.Show("编辑失败！", "失败", ExtAspNet.MessageBoxIcon.Error);
            }
        }

        protected void bnUpdate_OnClick(object sender, EventArgs e)
        {
            int id = int.Parse(Request.QueryString["id"]);
            Model.Consumable m = new Model.Consumable();
            m.id = id;
            m.name = lb_view_name.Text;
            m.normal = int.Parse(tb_update_normal.Text);
            m.bad = int.Parse(tb_update_bad.Text);
            m.stock = int.Parse(tb_update_stock.Text);
            m.remarks = lb_view_remarks.Text;
            m.location = int.Parse(hf_view_location.Text);
            DateTime dt = dp_update_date.SelectedDate.Value;
            if(ConsumableManage.Update(m,dt))
            {
                PageContext.RegisterStartupScript(Alert.GetShowInTopReference("更新记录成功！", "操作提示！", MessageBoxIcon.Information) + ActiveWindow.GetHidePostBackReference(Common.LinkService.RefreshString));
            }
            else
            {
                Alert.Show("更新记录失败！", "失败", ExtAspNet.MessageBoxIcon.Error);
            }
        }
        protected void bnAdd_OnClick(object sender, EventArgs e)
        {
            Model.Consumable m = new Model.Consumable();
            m.name = tb_add_name.Text;
            m.location = int.Parse(dl_add_location.SelectedValue);
            m.remarks = tb_add_remarks.Text;
            m.normal = int.Parse(tb_add_normal.Text);
            m.bad = int.Parse(tb_add_bad.Text);
            m.stock = int.Parse(tb_add_stock.Text);
            if(ConsumableManage.Add(m))
            {
                PageContext.RegisterStartupScript(Alert.GetShowInTopReference("新增成功！", "操作提示！", MessageBoxIcon.Information) + ActiveWindow.GetHidePostBackReference(Common.LinkService.RefreshString));
            }
            else
            {
                Alert.Show("新增遇到错误！", "失败", ExtAspNet.MessageBoxIcon.Error);
            }
        }
        protected void updateStock_OnTextChanged(object sender, EventArgs e)
        {
            tb_update_stock.Text = (int.Parse(tb_update_normal.Text.Trim()) + int.Parse(tb_update_bad.Text.Trim())).ToString();
        }
    }
}